package nero.yncc.com.createart.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;

import nero.yncc.com.createart.R;
import nero.yncc.com.createart.adapter.AdapterAddress;
import nero.yncc.com.createart.base.BaseActivity;
import nero.yncc.com.createart.base.Constants;
import nero.yncc.com.createart.bean.AddressBean;
import nero.yncc.com.createart.fragment.FragmentAd;
import nero.yncc.com.createart.utils.DialogUtil;
import nero.yncc.com.createart.utils.HttpUtils;


//import nero.yncc.com.createart.adapter.AdapterAddress;


@ContentView(R.layout.activity_address_manager)
public class ActivityAddressManager extends BaseActivity {

    @ViewInject(R.id.rc_address)
    RecyclerView rc_address;
    @ViewInject(R.id.tv_add_address)
    TextView tv_add_address;



    ArrayList<AddressBean> arrayList;
    boolean canClick;
    AdapterAddress adapter;
    @Override
    protected void initView() {
        rc_address.setLayoutManager(new LinearLayoutManager(mContext,LinearLayoutManager.VERTICAL, false));
        setTitleBarTitle("收货地址");
    }

    @Override
    protected void initData() {
        canClick=getIntent().getBooleanExtra("canClick",false);
        arrayList=new ArrayList<>();
        adapter = new AdapterAddress(arrayList, this, new OnAddressItemClickListener() {

            @Override
            public void onDefaultClick(AddressBean item) {
                setAddress2Default(item);
            }

            @Override
            public void onEditClick(AddressBean item) {
                Intent intent = new Intent(mContext, ActivityAddressEdit.class);
                intent.putExtra("address_id", item);
                startActivityForResult(intent,0);
            }

            @Override
            public void onDeleteClick(final AddressBean item) {
                DialogUtil.getConfirmDialog(mContext, "是否删除该地址?", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        deleteItem(item);
                    }
                }).show();
            }

            @Override
            public void OnAllClick(int id) {
                //这个是从订单列表跳过来选择的
                if (canClick){
                    Intent intent=new Intent();
                    intent.putExtra("bean",arrayList.get(id));
                    setResult(RESULT_OK,intent);
                    finish();
                }
            }
        });
        rc_address.setAdapter(adapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==RESULT_OK){
            getHttpData();
        }
    }

    @Override
    protected void initListener() {
        tv_add_address.setOnClickListener(this);
//        ivBack.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                finish();
//            }
//        });
//        ivFunction.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Intent intent = new Intent(this, AddressEdit.class);
//                intent.putExtra("flag", FLAG_FROM_ADD);
//                startActivity(intent);
//            }
//        });
//        lvAddmag.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                AddressBean item = (AddressBean) adapterView.getItemAtPosition(i);
//                if (com4Choose) {
//                    Intent intent = new Intent();
//                    intent.putExtra("address_id", String.valueOf(item.getAddress_id()));
//                    intent.putExtra("address", item.getProvincename() + item.getCityname() + item.getDistrictname() + item.getAddress());
//                    intent.putExtra("addressBean", item);
//                    setResult(RESULT_OK, intent);
//                    finish();
//                }
//            }
//        });
    }

    @Override
    protected void doClick(int id) {
        super.doClick(id);
        switch (id){
            case R.id.tv_add_address:
                Intent intent = new Intent(mContext, ActivityAddressEdit.class);

                startActivityForResult(intent,0);
                break;
        }
    }



    @Override
    protected void getHttpData() {
        super.getHttpData();

        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("user_id",userBean.getUser_id());
        new HttpUtils().post(this, Constants.API_ADDRESSLIST, hashMap, null, HttpUtils.TYPE_ARRAY, new HttpUtils.HttpCallback() {
            @Override
            public void success(String msg, Object json) {
                JSONArray jsonArray=(JSONArray)json;
                arrayList.clear();
                adapter.notifyDataSetChanged();
                for (int a=0;a<jsonArray.length();a++){
                    try {
                        arrayList.add(JSON.parseObject(jsonArray.getString(a),AddressBean.class));
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                adapter.notifyDataSetChanged();
                hideProgress();
            }

            @Override
            public void fail(String failMsg) {
                showToast(failMsg);
                hideProgress();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }


    /**
     * 设置为默认地址
     *
     * @param item
     */
    private void setAddress2Default(final AddressBean item) {
        HashMap<String,String> hashMap=new HashMap();
        hashMap.put("user_id",userBean.getUser_id());
        hashMap.put("address_id",item.getAddress_id());
        showProgress("正在设置默认地址...");
        new HttpUtils().post(this, Constants.API_DEFAULTADDRESS, hashMap, null, HttpUtils.TYPE_NOTHING, new HttpUtils.HttpCallback() {
            @Override
            public void success(String msg, Object json) {
                showToast(msg);
                getHttpData();
            }

            @Override
            public void fail(String failMsg) {
                showToast(failMsg);
                getHttpData();
            }
        });

    }

    /**
     * 删除地址
     *
     * @param item
     */
    private void deleteItem(final AddressBean item) {
        showProgress("正在删除地址...");
        HashMap<String,String> hashMap=new HashMap<>();
        hashMap.put("user_id",userBean.getUser_id());
        hashMap.put("id",item.getAddress_id());
        new HttpUtils().post(this, Constants.API_DELADDRESS, hashMap, null, HttpUtils.TYPE_NOTHING, new HttpUtils.HttpCallback() {
            @Override
            public void success(String msg, Object json) {
                showToast(msg);
                getHttpData();
            }

            @Override
            public void fail(String failMsg) {
                showToast(failMsg);
                getHttpData();
            }
        });


    }
    public interface OnAddressItemClickListener {
        void onDefaultClick(AddressBean item);
        void onEditClick(AddressBean item);
        void onDeleteClick(AddressBean item);
        void OnAllClick(int id);
    }
}
